using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace PropertyManagementFeatures
{
    class PropertyManagementTypes : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");

           CustomisePropertyDocumentLibrary();
            CustomisePropertyImageLibrary();

            BindEventHandlers();
            
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        protected void CustomisePropertyDocumentLibrary()
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {

                    SPList list = web.Lists["Property Documents"];
                    SPList targetList = web.Lists["Properties"];

                    list.Fields.AddLookup("Property", targetList.ID, false);
                    list.Update();

                    SPFieldLookup lkp = (SPFieldLookup)list.Fields["Property"];
                    lkp.Update();

                    list.Update();

                }
            }

        }

        protected void CustomisePropertyImageLibrary()
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {

                    SPList list = web.Lists["Property Pictures"];
                    SPList targetList = web.Lists["Properties"];

                    list.Fields.AddLookup("Property", targetList.ID, false);
                    list.Update();

                    SPFieldLookup lkp = (SPFieldLookup)list.Fields["Property"];
                    lkp.LookupField = targetList.Fields["Address"].InternalName;
                    lkp.Update();

                    list.Update();

                }
            }

        }

        protected void BindEventHandlers()
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["ViewingAppointments"];
                    string assemblyName = "PropertyManagementFeatures, Version=1.0.0.0, Culture=neutral, PublicKeyToken=49dee2d491642181";
                    string itemReceiverName = "PropertyManagementFeatures.FeatureCode.ViewingAppointmentItemEventReceiver";
               
                    list.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyName, itemReceiverName);
                    list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assemblyName, itemReceiverName);

                }
            }
        
        }
      
    }
}
